package com.hckj.medical.controller;


import com.hckj.medical.entity.UserInfo;
import com.hckj.medical.entity.Users;
import com.hckj.medical.service.IUserInfoService;
import com.hckj.medical.service.IUsersService;
import com.hckj.medical.utils.Code;
import com.hckj.medical.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hckj
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/medical/users")
@CrossOrigin
public class UsersController {
    @Autowired
    private IUsersService iUsersService;

    @Autowired
    private IUserInfoService iUserInfoService;

    //用户登录
    @PostMapping("/login")
    public R selectUsers(@RequestBody @Valid Users users, BindingResult result){
        String str = "";
        if (result.hasErrors()){
            for (ObjectError item:result.getAllErrors()
            ) {
                str = item.getDefaultMessage();
            }
            return new R(Code.LOGIN_ERR,str,"登录失败");
        }

        /**
         * 验证没有问题 开始将值传递给业务逻辑层进行验证登录
         */
        Users users1 = iUsersService.selectUsers(users.getPhone());
        if (users1==null){
            return new R(Code.LOGIN_ERR,"用户名不存在");
        }else if(users1.getPassword().equals(users.getPassword())){
            UserInfo userInfo = iUserInfoService.selectUserInfoByid(users1.getId());
            List<UserInfo> userInfoByid = iUserInfoService.findUserInfoByid(users1.getId());
            HashMap<Object, Object> ss = new HashMap<>();
            ss.put("userinfo",userInfo);
            ss.put("usercard",userInfoByid);
            return new R(Code.LOGIN_OK,ss,"登录成功");
        }else {
            return new R(Code.LOGIN_ERR,"密码错误");
        }
    }
    //切换就诊卡111
    @GetMapping("/cut/{id}")
    public R selectUser(@PathVariable Integer id){
        UserInfo userInfo = iUserInfoService.selectUserInfoByid(id);
        if (userInfo!=null){
            return new R(Code.GET_OK,userInfo,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }
    //添加救诊人 判断库中是否存在这个手机号
    @GetMapping("/phone/{phone}")
    public R slelct(@PathVariable String phone){
        Users users = iUsersService.selectUsers(phone);
        if (users!=null){
            return new R(Code.GET_OK,users,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }


    //添加救诊人 判断库中是否存在这个手机号
    @PostMapping("/insertUser")
    public R insertUsers(@RequestBody Users users){
        Integer integer = iUsersService.insertUsers(users);
        if (integer>0){
            return new R(Code.SAVE_OK,users.getId(),"添加成功");
        }else {
            return new R(Code.SAVE_ERRO,"添加成功");
        }
    }



//    工作台查询第一行数量
    @PostMapping("/selectWork")
    public R selectWork(@RequestBody Map map){
        Map bywork = iUsersService.findBywork(map);
        if (bywork.entrySet().size() > 0 && bywork != null){
            return new R(Code.GET_OK,bywork,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

}
